package com.hnevc.foodserver.controller;

import com.hnevc.foodserver.bean.*;
import com.hnevc.foodserver.service.FoodsService;
import io.swagger.annotations.*;
import net.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/food")
@Api(tags = "食物接口")
public class FoodsController {
    @Autowired
    FoodsService foodsService;
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "获取所有美食信息")
    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<>();
        try{
            List<Foods> all = foodsService.list();
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",all);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping("/findone")
    @ResponseBody
    @ApiOperation(value = "获取一项美食信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),
            @ApiImplicitParam(name = "name", value = "食物名称"),
            @ApiImplicitParam(name = "label", value = "简要描述"),
            @ApiImplicitParam(name = "icon", value = "食物图片"),
            @ApiImplicitParam(name = "price", value = "价格"),
            @ApiImplicitParam(name = "description", value = "详细描述"),
            @ApiImplicitParam(name = "type", value = "类别字符串")
    })
    public Map<String,Object> findone(@RequestBody Foods foods){
        Map<String,Object> map = new HashMap<>();
        try{
            Foods foods1 = foodsService.findone(foods);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",foods1);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }
    @PostMapping("/deleteone/{id}")
    @ResponseBody
    @ApiOperation(value = "删除一条记录，根据ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),

    })
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try{
            foodsService.deleteById(id);
            map.put("success",true);
            map.put("msg","成功");
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "更新一项美食信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),
            @ApiImplicitParam(name = "name", value = "食物名称"),
            @ApiImplicitParam(name = "label", value = "简要描述"),
            @ApiImplicitParam(name = "icon", value = "食物图片"),
            @ApiImplicitParam(name = "price", value = "价格"),
            @ApiImplicitParam(name = "description", value = "详细描述"),
            @ApiImplicitParam(name = "type", value = "类别字符串")
    })
    public Map<String,Object> update(@RequestBody Foods food){
        Map<String,Object> map = new HashMap<>();
        try{
            Foods savedFood = foodsService.updateFood(food);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",savedFood);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加一项美食信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "食物名称"),
            @ApiImplicitParam(name = "label", value = "简要描述"),
            @ApiImplicitParam(name = "icon", value = "食物图片"),
            @ApiImplicitParam(name = "price", value = "价格"),
            @ApiImplicitParam(name = "description", value = "详细描述"),
            @ApiImplicitParam(name = "type", value = "类别字符串")
    })
    public Map<String,Object> add(@RequestBody Foods food){
        Map<String,Object> map = new HashMap<>();
        try{
            Foods savedFood = foodsService.updateFood(food);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",savedFood);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @PostMapping("/upload_foodpic")
    @ApiOperation(value = "更新菜品图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "菜品图片"),
            @ApiImplicitParam(name = "id",value = "菜品ID")
    })
    public Map<String,Object> uploadFoodPic(@RequestParam("file") MultipartFile file,
                                            @RequestParam("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            byte[] bytes = file.getBytes();
            String filePath =  new RandomString(10).nextString() + file.getOriginalFilename();
            Path path = Paths.get("D://upload//" +filePath);
            Files.write(path, bytes);
            map.put("success",true);
            map.put("msg","成功");
            //TODO 更新到数据库
            // 1. 获取用户服务对象，
            Foods foods = foodsService.uploadIcon(id, filePath);
            map.put("data",foods);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","文件读取错误");
        }
        return map;
    }

    @PostMapping("/getFoodByPage")
    @ResponseBody
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
            @ApiImplicitParam(name = "postDate", value = "发表日期", defaultValue = "1999-09-09"),
    })

    public Map<String, Object> listByPage(@RequestBody FoodPageVo foodPageVo) {
        Map<String, Object> map = new HashMap<>();

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest page = PageRequest.of(foodPageVo.getPageIndex(), foodPageVo.getPageSize(), sort);

        Page<Foods> page1 = foodsService.getFoodByPage(page);
        if (page1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", page1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
}
